package com.avic.rong.utils;

import com.google.gson.Gson;
import com.tsy.sdk.myokhttp.MyOkHttp;
import com.tsy.sdk.myokhttp.response.IResponseHandler;
import com.tsy.sdk.myokhttp.response.JsonResponseHandler;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by weixuemeng on 2018/3/20.
 */

public class SmartHttp {

    public static void getHttpTwo(String url, final IResponseHandler mbackListener) {

        MyOkHttp mMyOkhttp = new MyOkHttp();
        SmartLog.log(url);
        mMyOkhttp.get()
                .url(url)
                .enqueue(mbackListener);

    }


    public static void getHttp(String url, final NetResponseListener mbackListener) {

        MyOkHttp mMyOkhttp = new MyOkHttp();
        SmartLog.log("---------------------" + url);
        mMyOkhttp.get()
                .url(url)
                .enqueue(new JsonResponseHandler() {

                    @Override
                    public void onSuccess(int statusCode, JSONObject response) {
                        SmartLog.log(response.toString() + "-----");
                        mbackListener.sucListener(response);
                    }

                    @Override
                    public void onFailure(int statusCode, String error_msg) {
                        mbackListener.errListener(error_msg);
                    }

                });

    }

    //POST 请求
    public static void postHttp(String url,JSONObject jsonObject,final NetResponseListener mbackListener){
        SmartLog.i("request body-----:", jsonObject.toString());
        MyOkHttp mMyOkhttp = new MyOkHttp();
        SmartLog.log(url);
        mMyOkhttp.post()
                .url(url)
                .jsonParams(jsonObject.toString())
                .enqueue(new JsonResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, JSONObject response) {
                        SmartLog.log(response.toString());
                        mbackListener.sucListener(response);
                    }
                    @Override
                    public void onSuccess(int statusCode, JSONArray response) {

                    }

                    @Override
                    public void onFailure(int statusCode, String error_msg) {
                        SmartLog.log(error_msg);
                        mbackListener.errListener(error_msg);
                    }
                });


    }

    //POST 请求
    public static void postHttpForm(String url,Map<String,String> map, final NetResponseListener mbackListener){
        SmartLog.i("request body-----:", new Gson().toJson(map));
        MyOkHttp mMyOkhttp = new MyOkHttp();
        SmartLog.log(url);
        mMyOkhttp.post()
                .url(url)
                .params(map)
                .enqueue(new JsonResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, JSONObject response) {
                        SmartLog.log(response.toString());
                        mbackListener.sucListener(response);
                    }
                    @Override
                    public void onSuccess(int statusCode, JSONArray response) {

                    }

                    @Override
                    public void onFailure(int statusCode, String error_msg) {
                        SmartLog.log(error_msg);
                        mbackListener.errListener(error_msg);
                    }
                });
    }



    //POST 请求
    public static void postHttp(String url,String jsonString,final NetResponseListener mbackListener){
        SmartLog.i("request body-----:", jsonString);
        MyOkHttp mMyOkhttp = new MyOkHttp();
        SmartLog.log(url);
        mMyOkhttp.post()
                .url(url)
                .jsonParams(jsonString)
                .enqueue(new JsonResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, JSONObject response) {
                        SmartLog.log(response.toString());
                        mbackListener.sucListener(response);
                    }
                    @Override
                    public void onSuccess(int statusCode, JSONArray response) {

                    }

                    @Override
                    public void onFailure(int statusCode, String error_msg) {
                        SmartLog.log(error_msg);
                        mbackListener.errListener(error_msg);
                    }
                });
    }


    public static void deleteHttp(String url, final NetResponseListener mbackListener){

        MyOkHttp mMyOkhttp = new MyOkHttp();

        mMyOkhttp.delete()
                .url(url)
                .enqueue(new JsonResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, JSONObject response) {
                        SmartLog.log(response.toString());
                        mbackListener.sucListener(response);
                    }

                    @Override
                    public void onFailure(int statusCode, String error_msg) {
                        SmartLog.log(error_msg);
                        mbackListener.errListener(error_msg);

                    }
                });
    }






    public static String postHttpSyncronize(String url,JSONObject jsonObject) {

        try{
            OkHttpClient client = new OkHttpClient();
            Request.Builder builder = new Request.Builder().url(url);
            RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonObject.toString());
            builder.post(body);
            Call call =  client.newCall(builder.build());
            Response response =  call.execute() ;
            String result =  response.body().string();
            Gson gson = new Gson();
            return "";
        }catch (Exception e){
            return "";
        }


    }






}
